﻿using System;
using log4net;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ODM2KMZ
{
    public partial class HIS2RSS : Page
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(HIS2RSS));
        public string CurrentURL  { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void Page_PreRender(object sender, EventArgs e)
        {


        }

        private WaterOneFlow initWaterOneFlow()
        {

            var waterOneFlow = new WaterOneFlow {Url = CurrentURL, Timeout = 600000, EnableDecompression = true};

            
            return waterOneFlow;

        }

        public void Page_Error(object sender, EventArgs e)
        {
            Exception objErr = Server.GetLastError().GetBaseException();
            Session["lastError"] = objErr.Message;
            Response.Redirect("ErrorHandler.aspx");
            Server.ClearError();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            
           
            CurrentURL = txtWaterOneFlow.Text;
   

            SiteInfoResponseType sites;

            var waterOneFlow = initWaterOneFlow();
            var sitearray = new[] { string.Empty };

           
            try
            {
                sites = waterOneFlow.GetSites(sitearray, string.Empty);

            }
            catch (Exception ex)
            {

                log.Debug("Exception Caught line 375 HIS2RSS.aspx.cs " + ex.Message);
                throw new Exception("Unable to get sites data from " + CurrentURL + "<BR/>" +
                                    ex.Message);
            }
            SiteInfoResponseTypeSite[] siteInfoResponseTypeSites = sites.site;
            //Setup Table Header
            var tr = new TableRow();

            var tc = new TableCell() { Text = "HIS Site Description" };
            tr.Cells.Add(tc);
            tc = new TableCell() { Text = "Click on logo to suscribe" };
            tr.Cells.Add(tc);
            tr.CssClass = "clsSectionHeading";
            tblMain.Rows.Add(tr);

            for (int i = 0; i < siteInfoResponseTypeSites.Length; i++)
            {
                
                SiteInfoType siteInfoType = siteInfoResponseTypeSites[i].siteInfo;
                SiteInfoTypeSiteCode siteInfoTypeSiteCode = siteInfoType.siteCode[0];
     

                try
                {

                     tr = new TableRow();

                    if ( i%2 == 1)
                    {
                        tr.CssClass = "clsSectionText";
                    }
                    else
                    {
                        tr.CssClass = "clsSectionTextAlt";
                    }

                    var imag = new Image() ;
                    imag.ImageUrl = "images/RSS.gif";
                    imag.Height = Unit.Pixel(32);
                    var HL = new HyperLink();
                    HL.ImageUrl = "images/RSS.gif";
                    HL.Text = "";
                    HL.NavigateUrl = "HIS2RSSStreamer.aspx?URL=" + CurrentURL +
                        "&SiteCode=" + siteInfoType.siteCode[0].network + ":" +
                        siteInfoType.siteCode[0].Value + 
                        "&NDAYS=" + ddlNDays.SelectedValue +
                        "&TTL=" + ddlTTL.SelectedValue +
                        "&DISPLAY_NO_DATA=" + chkBoxNoData.Checked;
                    HL.Height = Unit.Pixel(16);

                    
                    
                     tc = new TableCell() { Text = siteInfoType.siteName };
                    
                    tr.Cells.Add(tc);

                    tc = new TableCell();
                    tc.Controls.Add(HL);
                    tc.HorizontalAlign = HorizontalAlign.Center;
                    tr.Cells.Add(tc);
       

                    tblMain.Rows.Add(tr);




                }
                catch (Exception ex)
                {
                    log.Debug("Exception Caught line 375 RSS.aspx.cs " + ex.Message);
                    throw new Exception("Unable to get sites data from " + CurrentURL + "<BR/>" +
                                        ex.Message);

                }
            }
        }
        
    }
}
